package com.seed.core.field;

import com.seed.util.RowKit;

import java.util.LinkedHashMap;
import java.util.Map;

public class FieldContext {

    private static Map<String, FieldAuth> cache = new LinkedHashMap<>();

    static {
        cache.put("加1", new FieldAuth("加1", "就是加一个的意思", (entity, finalColumn) -> {
            RowKit.setSelfValue(entity, finalColumn, RowKit.getSelfValue(entity, finalColumn) + "*周");
        }));
        cache.put("加2", new FieldAuth("加2", "就是加两个的意思", (entity, finalColumn) -> {
            RowKit.setSelfValue(entity, finalColumn, RowKit.getSelfValue(entity, finalColumn) + "2");
        }));
    }

    public static void auth(String value, Object entity, String finalColumn) {

        if(value.startsWith("=")) {
            FieldAuth fa = cache.get(value.substring(1));
            if(fa == null) return;

            fa.getValue().auth(entity, finalColumn);
        } else {
//            SeedUtil.setForConst(entity, finalColumn, var);
            authConst(value, entity, finalColumn);
        }
    }

    private static void authConst(String value, Object entity, String finalColumn) {
        if(finalColumn.contains(".")) {
            // TODO
        } else {
            System.out.println("value");
            System.out.println(value);
            RowKit.setSelfValue(entity, finalColumn, value);
        }
    }

    public static LinkedHashMap<String, String> fieldAuths() {
        LinkedHashMap<String, String> ret = new LinkedHashMap<>();
        for(Map.Entry<String, FieldAuth> entry: cache.entrySet()) {
            ret.put(entry.getKey(), entry.getValue().getComment());
        }

        return ret;
    }
}
